﻿#include <Sys\RenderUnit\SysRenderDatagram\SOLIDRD.HPP>
#include <Render\Render_Win32\Render_Win32_OpenGL\RENDER_WIN32_OPENGL.HPP>
#include <Sys\RenderUnit\SYSDDCLASSID.HPP>

using namespace CxxlMan;
using namespace Cxxl3D;


class CSolidDD_OpenGL:public IDatagramDisposal_OpenGL
{
  virtual bool cxxlFASTCALL Exec(const Smart_Ptr<IRenderDatagramDisposalCenterOpengl> &IRenderDatagramDisposalCenter_Arg,
    const Smart_Ptr<IRenderDatagram> &IRenderDatagram_Arg, const CxxlMan::CSmart_Ptr<Matrix4x4> &ViewMatrix_Arg)
  {

    Smart_Ptr<ISolidRD> ISolidRD_Ptr = (ISolidRD*)GetDefaultInterface(IRenderDatagram_Arg);
    Smart_Ptr<IC3D_Node> IC3D_Node_Ptr = ISolidRD_Ptr->Get_IC3D_Node();
    CSmart_Ptr<const Matrix4x4> WorldMatrix_Ptr = IC3D_Node_Ptr->GetWorldMatrix();

    Matrix4x4 WorldViewMatrix = (*WorldMatrix_Ptr) * (*ViewMatrix_Arg);
    glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf((C3D_FLOAT*)(Matrix4x4*)&WorldViewMatrix); // Set Modeview matrix


    tagISolidRD_Cfg Cfg = ISolidRD_Ptr->GetCfg();

    if(Cfg.Light == 1)
      glEnable(GL_LIGHTING);
    else
      glDisable(GL_LIGHTING);

    // glFrontFace(GL_CCW);

    DWORD CullMode;
    switch(Cfg.CullMode)
    {
    case 0:
      glDisable(GL_CULL_FACE);
      break;
    case 1:
      glEnable(GL_CULL_FACE);
      glCullFace(GL_BACK);
      break;
    case 2:
      glEnable(GL_CULL_FACE);
      glCullFace(GL_FRONT);
    }

   // Smart_Ptr<IBasicPrimitiveRD> IBasicPrimitiveRD_Ptr = ISolidRD_Ptr->GetIBasicPrimitiveRD();

  //  return IRenderDatagramDisposalCenter_Arg->Exec((IBasicPrimitiveRD*)IBasicPrimitiveRD_Ptr,ViewMatrix_Arg);
    return true;

  }

public:
  // Constructor
  CSolidDD_OpenGL()
    :cxxlObject(Spirit_Easy)
  {
  }

  // Destructor
  ~CSolidDD_OpenGL()
  {
  }

};

cxxlObject *cxxlFASTCALL New_CSolidDD_OpenGL()
{
  return new CSolidDD_OpenGL;
}